package com.classic.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.classic.duanzhi.PrivacyPolicyActivity;
import com.classic.duanzhi.R;


public class UtilsPrivacyPolicy extends LinearLayout {
    public UtilsPrivacyPolicy(Context context) {
        super(context);
        initView(context, null);
    }

    public UtilsPrivacyPolicy(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public UtilsPrivacyPolicy(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private boolean isFirst = true;             //是否第一次进入

    private boolean isOnClick = true;           //是否点击过 选择框
    private OnUtilsPrivacyPolicyListener listener; //  监听
    private TextView v;                             //《个人信息保护政策》文案 主要设置颜色用
    private TextView next;                          //允许按钮
    private View checkBox;                          //点击选择框
    private View view;                              //当前页面布局

    private void initView(final Context context, AttributeSet o) {
        LayoutInflater.from(context).inflate(R.layout.u_privacy_policy_layout, this);
        view = findViewById(R.id.u_ll_layout);
        next = findViewById(R.id.u_tv_next);
        checkBox = findViewById(R.id.u_tv_checkBox);
        v = findViewById(R.id.u_tv_yinshi);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PrivacyPolicyActivity.starActivity( (Activity) context);
            }
        });
        final SharedPreferences sharedPreferences = context.getSharedPreferences("FirstRun", 0);
        Boolean first_run = sharedPreferences.getBoolean("First", true);
        if (!first_run) {
            if (null != listener)
                listener.onPermission();
            view.setVisibility(GONE);
            isFirst = false;
            return;
        }
        checkBox.setSelected(isOnClick);
        checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                isOnClick = !isOnClick;
                v.setSelected(isOnClick);
            }
        });
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOnClick) {
                    sharedPreferences.edit().putBoolean("First", false).apply();
                    if (null != listener) listener.onPermission();
                    view.setVisibility(GONE);
                } else {
                    Toast.makeText(context, "您还未同意个人信息保护政策", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    public OnUtilsPrivacyPolicyListener getOnUtilsPrivacyPolicyListener() {
        return listener;
    }

    /**
     * 设置监听
     */
    public void setOnUtilsPrivacyPolicyListener(OnUtilsPrivacyPolicyListener listener) {
        this.listener = listener;
        if (!isFirst) {
            listener.onPermission();
        }
    }

    /**
     * 允许回调监听
     */
    public interface OnUtilsPrivacyPolicyListener {
        /**
         * 允许进入
         */
        void onPermission();
    }
}
